library(tidyverse)
library(haven)
library(texreg)
library(broom)
library(margins)
library(dotwhisker)

ex <- read_rds("../../data/replication/experiment_data_tidied.rds") 

outputs_path <- "../../outputs/replication/"

source("functions_exp_models.R")

source("texreg_variable_list.R")

mod_1a <- lm(uk_covid_performance_w21_num ~ treatment_w21 * party_id_gov_op_w21 +
    male_w21 + white_british_w21 + age_w21 + class_w21 + education_w21,
  data = (ex %>% filter(rm_for_party != 1) %>%
            filter(rm_for_vaccine_attitude != 1)),
  weights = wt_with_pol_att_w21)

mod_1b <- MASS::polr(uk_covid_performance_w21 ~ treatment_w21 * party_id_gov_op_w21 +
    male_w21 + white_british_w21 + age_w21 + class_w21 + education_w21,
  data = (ex %>% filter(rm_for_party != 1) %>%
            filter(rm_for_vaccine_attitude != 1)),
  weights = wt_with_pol_att_w21)

mod_2a <- lm(retro_handle_w21_num ~ treatment_w21 * party_id_gov_op_w21 +
    male_w21 + white_british_w21 + age_w21 + class_w21 + education_w21,
  data = (ex %>% filter(rm_for_party != 1) %>%
            filter(rm_for_vaccine_attitude != 1)),
  weights = wt_with_pol_att_w21)

mod_2b <- MASS::polr(retro_handle_w21 ~ treatment_w21 * party_id_gov_op_w21 +
    male_w21 + white_british_w21 + age_w21 + class_w21 + education_w21,
  data = (ex %>% filter(rm_for_party != 1) %>%
            filter(rm_for_vaccine_attitude != 1)),
  weights = wt_with_pol_att_w21)


# defining function in file since might want different coef maps or 
# scale boxes for different models
custom_texreg <- function(list_of_models, ...){
  customised_reg <- generic_tidy_texreg(list_of_models,
    sideways = FALSE,
    custom.note = ("\\normalsize{%stars}"),
    scalebox = 0.6,
    ...)

  return(customised_reg)
}

# get the right models based on choice defined at top of document

caption_suffix <- paste("Results from ordered logit regressions of handling",
                        "variables on treatment status",
                        "interacted with party ID")

custom_texreg(list(mod_1a, mod_1b, mod_2a, mod_2b),
       file = paste0(outputs_path, "ordered_logit_comparison.tex"),
       custom.header = list("\\textit{Overall performance}" = c(1:2),
                            "\\textit{Retro handling}" = c(3:4)),
       custom.coef.map = texreg_variable_list,
       custom.model.names = c("OLS", "Ord. Log", "OLS", "Ord. Log"),
       caption = paste0(caption_suffix, ", with full controls added"),
       label = "tab: ordered_logit_compare",
       groups = list("Partisanship: (ref = Opposition)" = 1:2,
                     "Treatment: (ref = Control)" = 3:4,
                     "Conditionals" = 9:12,
                     "Age: (ref = Under 18)" = 15:17,
                     "Class: (ref = Higher Managerial)" = 18:22,
                     "Education: (ref = Undergrad or Higher)" = 23:26))

mod_3a <- lm(uk_covid_performance_w21_num ~ party_id_gov_op_w19 +
    male_w21 + white_british_w21 + age_w21 + class_w21 + education_w21,
  data = (ex %>% filter(rm_for_party != 1) %>%
            filter(rm_for_vaccine_attitude != 1)),
  weights = wt_with_pol_att_w21)

mod_3b <- MASS::polr(uk_covid_performance_w21 ~ party_id_gov_op_w19 +
    male_w21 + white_british_w21 + age_w21 + class_w21 + education_w21,
  data = (ex %>% filter(rm_for_party != 1) %>%
            filter(rm_for_vaccine_attitude != 1)),
  weights = wt_with_pol_att_w21)

mod_4a <- lm(retro_handle_w21_num ~ party_id_gov_op_w19 +
    male_w21 + white_british_w21 + age_w21 + class_w21 + education_w21,
  data = (ex %>% filter(rm_for_party != 1) %>%
            filter(rm_for_vaccine_attitude != 1)),
  weights = wt_with_pol_att_w21)

mod_4b <- MASS::polr(retro_handle_w21 ~ party_id_gov_op_w19 +
    male_w21 + white_british_w21 + age_w21 + class_w21 + education_w21,
  data = (ex %>% filter(rm_for_party != 1) %>%
            filter(rm_for_vaccine_attitude != 1)),
  weights = wt_with_pol_att_w21)

# get the right models based on choice defined at top of document

caption_suffix <- paste("Results from ordered logit regressions of handling",
                        "variables on party ID")

custom_texreg(list(mod_3a, mod_3b, mod_4a, mod_4b),
       file = paste0(outputs_path, "panel_data_ordered_logit_comparison.tex"),
       custom.header = list("\\textit{Overall performance}" = c(1:2),
                            "\\textit{Retro handling}" = c(3:4)),
       custom.model.names = c("OLS", "Ord. Log", "OLS", "Ord. Log"),
       caption = paste0(caption_suffix, ", with full controls added"),
       custom.coef.map = texreg_variable_list,
       label = "tab: ordered_logit_compare",
       groups = list("Partisanship: (ref = Opposition)" = 1:2,
                     "Conditionals" = 3:6,
                     "Age: (ref = Under 18)" = 9:11,
                     "Class: (ref = Higher Managerial)" = 12:17,
                     "Education: (ref = Undergrad or Higher)" = 18:21))



